const emailInput = document.getElementById('email');
const passwordInput = document.getElementById('password');
const captchaInput = document.getElementById('captcha');
const passwordLoginDiv = document.getElementById('password-login');
const codeLoginDiv = document.getElementById('code-login');
const switchToCode = document.getElementById('switchToCode');
const switchToPassword = document.getElementById('switchToPassword');

let loginType = 'password'; // password 或 code

switchToCode.addEventListener('click', function() {
    loginType = 'code';
    passwordLoginDiv.style.display = 'none';
    codeLoginDiv.style.display = '';
    switchToCode.style.display = 'none';
    switchToPassword.style.display = '';
});
switchToPassword.addEventListener('click', function() {
    loginType = 'password';
    passwordLoginDiv.style.display = '';
    codeLoginDiv.style.display = 'none';
    switchToCode.style.display = '';
    switchToPassword.style.display = 'none';
});

document.getElementById('loginForm').addEventListener('submit', function(event) {
    event.preventDefault();
    const email = emailInput.value.trim();
    const password = passwordInput.value.trim();
    const code = captchaInput.value.trim();
    if (!email) {
        alert('请输入邮箱');
        return;
    }
    if (loginType === 'password') {
        if (!password) {
            alert('请输入密码1');
            return;
        }
        fetch('/api/user/password/login', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ password, email })
        })
        .then(res => res.json())
        .then(result => {
            if (result.code === 200) {
                if (result.data) localStorage.setItem('token', result.data);
                alert('登录成功！');
                window.location.href = '../html/main-page.html';
            } else {
                alert(result.message || '登录失败');
            }
        })
        .catch(() => alert('登录失败，请重试'));
    } else {
        if (!code) {
            alert('请输入邮箱验证码');
            return;
        }
        // 调用验证码登录接口
        fetch('/api/user/code/login', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ email, code })
        })
        .then(res => res.json())
        .then(result => {
            if (result.code === 200) {
                if (result.data) localStorage.setItem('token', result.data);
                alert('登录成功！');
                window.location.href = '../html/main-page.html';
            } else {
                alert(result.message || '验证码登录失败');
            }
        })
        .catch(() => alert('验证码登录失败，请重试'));
    }
});

document.getElementById('sendCodeBtn').addEventListener('click', function() {
    const email = emailInput.value.trim();
    if (!email) {
        alert('请先输入邮箱');
        return;
    }
    const btn = this;
    btn.disabled = true;
    btn.textContent = '发送中...';
    fetch(`/api/user/send/${encodeURIComponent(email)}`)
        .then(res => res.json())
        .then(result => {
            if (result.code === 200) {
                alert('验证码已发送，请查收邮箱');
                let sec = 60;
                btn.textContent = `重新发送(${sec})`;
                const timer = setInterval(() => {
                    sec--;
                    btn.textContent = `重新发送(${sec})`;
                    if (sec <= 0) {
                        clearInterval(timer);
                        btn.disabled = false;
                        btn.textContent = '发送验证码';
                    }
                }, 1000);
            } else {
                alert(result.message || '发送失败');
                btn.disabled = false;
                btn.textContent = '发送验证码';
            }
        })
        .catch(() => {
            alert('发送失败，请重试');
            btn.disabled = false;
            btn.textContent = '发送验证码';
        });
});